;
(function(S) {
	S.mixin({
		/**
		 * 遍历一个数组或者对象，并执行回调函数
		 * @method each
		 * @param {Object | Array} object 被遍历的对象
		 * @param {Function} callback 执行的回调函数
		 * @param {Object} conext 回调函数内部的作用域
		 * @return {Object} object 被遍历的对象
		 */
		each : function(object, callback, context) {
			var name, i = 0, length = object.length;
			if (length === undefined) {
				for (name in object) {
					if (callback.call(context || object[name], object[name], name) === false) {
						break;
					}
				}
			} else {
				for (var value = object[0]; i < length && callback.call(context || object[name], value, i) !== false; value = object[++i]) {
				}
			}
			return object;
		},
		/**
		 * 声明一个命名空间
		 *  <p>
		 *  <code>
		 *  Simple.namespace("Simple.dd");
		 *  Simple.dd.Drag=function(){}
		 *  </code>
		 *  </p>
		 * @method namespace
		 * @param {string} arguments
		 * @return {object} o 根据给出的字符串创建的对象
		 */
		namespace : function() {
			var a = arguments, o = this, i = 0, j, d, arg;
			for (; i < a.length; i++) {
				arg = a[i];
				if (arg.indexOf(".")) {
					d = arg.split(".");
					for ( j = (d[0] == 'Simple') ? 1 : 0; j < d.length; j++) {
						o[d[j]] = o[d[j]] || {};
						o = o[d[j]];
					}
				} else {
					o[arg] = o[arg] || {};
				}
			}
			return o;
		},
		/**
		 * 在控制台输出信息
		 * @method log
		 * @param {string} msg 需要输出的内容
		 */
		log : function(msg) {
			if (window.console && console.log) {
				console.log.apply(this, msg);
			}
		},
		/**
		 * 抛出错误
		 * @method error
		 * @param {string} 错误内容
		 */
		error : function(msg) {
			throw msg;
		}
	});

	//size
	S.mixin({
		/**
		 * 获取当前页面的宽高(加上滚动条部分)
		 * @method getDoc
		 * @return {Object} Object.h 高度
		 * @return {Object} Object.w 宽度
		 */
		getDoc : function() {
			return {
				h : Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight),
				w : Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth)
			}
		},
		/**
		 * 获取当前页面的可视宽高
		 * @method getClient
		 * @return {Object} Object.h 高度
		 * @return {Object} Object.w 宽度
		 */
		getClient : function() {
			return {
				h : document.documentElement.clientHeight,
				w : document.documentElement.clientWidth
			}
		},
		/**
		 * 获取当前页面的可视宽高
		 * @method getSize
		 * @param el {Object} 需要后去宽高的HTML对象
		 * @param isModal {Boolean} 是否获取整个高度(加上滚动条部分)
		 * @return {Object} Object.h 高度
		 * @return {Object} Object.w 宽度
		 */
		getSize : function(el, isModal) {
			if (!( el instanceof jQuery)) {
				el = $(el);
			}
			if (el.is("body")) {
				if (isModal) {
					return this.getDoc();
				} else {
					return this.getClient();
				}
			} else {
				return {
					h : el.outerHeight(),
					w : el.outerWidth()
				}
			}
		}
	});

	/**
	 * 获取对象位置信息
	 * @module Simple.position
	 */
	S.position = {
		/**
		 * 设置某个对象相对参考对象的位置
		 * @method at
		 * @param config {Object}
		 * @param config.node {Object} 需要设置位置的对象
		 * @param config.referNode {Object} 相对对象
		 * @param config.points {String} 可选参数(cc | rc | bc | lc)
		 * @param config.offset {String} '0,5'
		 */
		at : function(config) {
			var node = config.node, referNode = config.referNode, points = config.points || 'cc', offset = config.offset || '0 0';
			node = $(node);
			offset = offset.split(' ');
			if (node.css("position") == "static") {
				node.css("position", "absolute");
			}
			var rfOffset = $(referNode).offset(), w = node.outerWidth(), h = node.outerHeight(), t, l, rfw = S.getSize(referNode).w, rfh = S.getSize(referNode).h;
			if ( typeof points == "string") {
				switch (points) {
					case "cc":
						t = rfOffset.top + (rfh - h) / 2;
						l = rfOffset.left + (rfw - w) / 2;
						break;
					case "rc":
						t = rfOffset.top + (rfh - h) / 2;
						l = rfOffset.left + rfw;
						break;
					case "bc":
						t = rfOffset.top + rfh;
						l = rfOffset.left + (rfw - w) / 2;
						break;
					case "lc":
						t = rfOffset.top + (rfh - h) / 2;
						l = rfOffset.left - w;
						break;
					case "tc":
						t = rfOffset.top - h;
						l = rfOffset.left + (rfw - w) / 2;
						break;
				}
			}
			t = t + offset[1];
			l = l + offset[0];
			node.css({
				top : t,
				left : l
			});
		},
		/**
		 * 让某个元素 相对body局中
		 * @method atBody
		 * @param node {Object} 需要获取位置的对象
		 */
		atBody : function(node) {
			var node = $(node), pos = node.css("position"), doc = Simple.getSize(document.body);
			if (pos == "fixed") {
				node.css({
					top : (doc.h - node.height()) / 2,
					left : (doc.w - node.width()) / 2
				});
			} else if (pos == "absolute") {
				node.css({
					top : (doc.h - node.height()) / 2 + Math.max(document.documentElement.scrollTop, document.body.scrollTop),
					left : (doc.w - node.width()) / 2
				});
			}
		}
	};

	S.mixin({
		/**
		 * 获取对象的绝对位置
		 * @method getRealPos
		 * @param {Object} element 需要获取位置的对象
		 * @return {Object} 返回对象的top, left 值
		 */
		getRealPos : function(element) {
			var position = element.css("position"), top, left, parentOffset, offset = element.offset(), right, bottom;
			if (position == "static") {
				element.css("position", "relative");
				top = 0;
				left = 0;
			} else if (position == "absolute") {
				var parent = element.parent()
				while (parent.css("position") == "static" && !parent.is("body")) {
					parent = parent.parent();
				}
				parentOffset = parent.offset();
				top = offset.top - parentOffset.top;
				left = offset.left - parentOffset.left;
			} else if (position == "fixed") {
				top = parseFloat(element.css("top"));
				left = parseFloat(element.css("left"));
				if (isNaN(top)) {
					top = offset.top - Math.max(document.documentElement.scrollTop, document.body.scrollTop);
				}
				if (isNaN(left)) {
					left = offset.left;
				}
			} else if (position == "relative") {
				top = parseFloat(element.css("top"));
				left = parseFloat(element.css("left"));
				right = parseFloat(element.css("right"));
				bottom = parseFloat(element.css("bottom"));
				if (isNaN(top)) {
					if (!isNaN(bottom)) {
						top = -bottom
					} else {
						top = 0;
					}
				}
				if (isNaN(left)) {
					if (!isNaN(right)) {
						left = -right
					} else {
						left = 0;
					}
				}
			}

			return {
				left : left,
				top : top
			}
		},

		/**
		 * 获取元素的 left top right bottom
		 * @method getCoordinates
		 * @param {Object} element 需要获取位置的对象
		 * @return {Object} 返回对象的left top right bottom值
		 */
		getCoordinates : function(element) {
			var coor = {};
			element = $(element);
			if (element.length < 1) {
				return;
			}
			var offset = element.offset(), right = element.outerWidth() + offset.left, bottom = element.outerHeight() + offset.top;

			S.mixin(coor, {
				right : right,
				bottom : bottom
			}, offset);

			return coor;
		},
		/**
		 * 获取浏览器窗口滚动条位置
		 * @method getWinScroll
		 * @return {Object} 返回滚动条的值 x,y
		 */
		getWinScroll : function() {
			return {
				x : Math.max(document.documentElement.scrollLeft, document.body.scrollLeft),
				y : Math.max(document.documentElement.scrollTop, document.body.scrollTop)
			}
		}
	});

	//cookie
	S.namespace('cookie');
	/**
	 * cookie模块负责cookie的读写操作
	 * @module cookie
	 */
	S.mixin(S.cookie, {
		/**
		 * 获取cookie的值
		 * @method get
		 * @param {String} name
		 * @return cookie的值
		 */
		get : function(name) {
			var cookieValue = "";
			var search = name + "=";
			if (document.cookie.length > 0) {
				offset = document.cookie.indexOf(search);
				if (offset != -1) {
					offset += search.length;
					end = document.cookie.indexOf(";", offset);
					if (end == -1)
						end = document.cookie.length;
					cookieValue = unescape(document.cookie.substring(offset, end));
				}
			}
			return cookieValue;
		},
		/**
		 * 设置cookie的值
		 * @method set
		 * @param {String} name
		 * @param {String} value
		 * @param {Number} hours
		 */
		set : function(name, value, hours) {
			var expire = "";
			if (hours != null) {
				expire = new Date((new Date()).getTime() + hours * 3600000);
				expire = "; expires=" + expire.toGMTString();
			}
			document.cookie = name + "=" + escape(value) + expire + ";path=/";
		}
	});
	/**
	 * 将Simple下的插件桥接到jQuery
	 * @method bridgeTojQuery
	 * @param methodName {String} 插件名称
	 * @param widget {Object} 对应的插件
	 */
	S.bridgeTojQuery = function(methodName, widget) {
		var methodArray = methodName.split(",");
		jQuery.each(methodArray, function(i, n) {
			jQuery.fn[n] = function(config) {
				return this.each(function() {
					if (config == "destroy") {
						if (jQuery.data(this, "widget-" + n)) {
							jQuery.data(this, "widget-" + n).destroy();
							$(this).removeData("widget-" + n);
							return;
						} else {
							return true;
						}
					}
					config = config || {};
					config = S.mixin(config, {
						node : this
					});
					//不要重复绑定
					if (jQuery.data(this, "widget-" + n)) {
						return;
					}
					var widgetInc = new widget(config);
					jQuery.data(this, "widget-" + n, widgetInc);
				});
			}
		});
	};
})(Simple);
